// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command
const $ = _.aggregate
// 云函数入口函数
exports.main = async (event, context) => {
    let message = cloud.getWXContext()
    if (event.sort == "usedGoods") {
        var type = {} //排序方式
        var agg = {}  //过滤方式
        if (event.type == "最新发布") {
            type = {
                time: -1,
            }
        } else if (event.type == "价格高") {
            type = {
                price: -1,
            }
        } else if (event.type == "价格低") {
            type = {
                price: 1,
            }
        } else {  //排序
            type = {
                time: 1,
            }
        }


        if (event.seekValue && event.category == "全部") {
            //搜索
            return await db.collection('usedGoods_list').aggregate()
                .match(
                    _.and([
                        {
                            title: db.RegExp({
                                regexp: event.seekValue,
                                options: 'i',
                            })
                        },
                        { _openid: _.not(_.eq(message.OPENID)) }
                    ])
                )
                .sort(type).skip(event.skip).limit(10).end()

        } else if (!event.seekValue && event.category != "全部") {
            //没有搜索时，有分类
            return await db.collection('usedGoods_list').aggregate()
                .match(
                    _.and([
                        { category: event.category },
                        { _openid: _.not(_.eq(message.OPENID)) }
                    ])
                )
                .sort(type).skip(event.skip).limit(10).end()
        } else if (event.seekValue && event.category != "全部") {
            //有搜索时，分类
            return await db.collection('usedGoods_list').aggregate()
                .match(
                    _.and([
                        {
                            title: db.RegExp({
                                regexp: event.seekValue,
                                options: 'i',
                            })
                        },
                        { category: event.category },
                        { _openid: _.not(_.eq(message.OPENID)) }
                    ])
                )
                .sort(type).skip(event.skip).limit(10).end()
        } else {
            return await db.collection('usedGoods_list').aggregate().match(
                { _openid: _.not(_.eq(message.OPENID)) }
            ).sort(type).skip(event.skip).limit(10).end()
        }

    } else if (event.sort == "lostGoods") {
        if (event.date == "") {
            return await db.collection('lostGoods_list').where({
                _openid: _.not(_.eq(message.OPENID))
            }).skip(event.skip).limit(10).get()
        } else {
            let time = new Date(event.date).setTime(new Date(event.date).getTime() + 24 * 60 * 60 * 1000);
            return await db.collection('lostGoods_list').where({
                'time': _.and(_.gte(new Date(event.date)), _.lt(new Date(time))),
                _openid: _.not(_.eq(message.OPENID))
            }).skip(event.skip).limit(10).get()
        }

    } else {
        return "error"
    }

}